package com.blackman.skcrawler.data.service;

import com.blackman.skcrawler.core.crawler.po.WormPageRegular;
import com.blackman.skcrawler.data.mapper.WormPageRegularMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 网页抓取正则serviceImpl业务实现
 * @author staywood
 * @date 2018-04-18 13:25
 * @(C)  Copyright 
 */
@Service("wormPageRegularService")
public class WormPageRegularServiceImpl extends BaseServiceImpl<WormPageRegularMapper, WormPageRegular>{

    /**
     * 新增正则匹配
     * @param regularList
     */
    public void addOrUpdate(List<String> regularList, String regularType) {
        if(regularList == null || regularList.size() == 0){
            return;
        }
        for(int i = 0;i < regularList.size();i++){
            String regularStr = regularList.get(i);
            WormPageRegular regular = new WormPageRegular();
            regular.setRegularUrl(regularStr);
            regular.setType(regularType);
            int count = selectCountByEntity(regular);
            if(count != 0){
                continue;
            }
            regular.setType(regularType);
            regular.setCreateTime(new Date());
            regular.setModifyTime(new Date());
            regular.setEnabled(false);
            insert(regular);
        }
    }

    /**
     * 获取所有类型正则
     * @return
     */
    public List<String> findTypeList(String type) {
        WormPageRegular regular = new WormPageRegular();
        regular.setType(type);
        List<WormPageRegular> regularList = selectListByEntity(regular);
        try {
            List<String> strList = new ArrayList<String>();
            for(WormPageRegular regular1:regularList){
                strList.add(regular1.getRegularUrl());
            }
            return strList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
